package com.pet.server.ext.action.tuangou;

import net.sf.json.JSONObject;
import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.baidu.DistanceEstimator;
import com.pet.server.ext.util.nuomi.NuomiClient;

public class Shopinfo extends ActionBase{

	public String shop_id,source;
	Double longitude,latitude;
	public Shopinfo(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		shop_id=params.getFieldValue("shop_id");
		source=params.getFieldValue("source");
		longitude=NumberUtil.getDouble(params.getFieldValue("longitude"));
		latitude=NumberUtil.getDouble(params.getFieldValue("latitude"));
	    if(StringUtil.isEmpty(shop_id,source)){
	    	throw ApiError.Type.INVALID_PARAM.toException();
	    }
	
	}
	
	@Override
	public Result invokeService() throws ApiError {
		JSONObject json = new JSONObject();
		if("nuomi".equals(source)){
			json=getNuomiJson(shop_id);
		}
    	Result result = new Result();
    	result.put("data", json);
	    return result;
	}

	public JSONObject getNuomiJson(String shop_id) throws ApiError{
		JSONObject dataJson = NuomiClient.shopinfo(shop_id);
		Integer errno = JsonUtil.getValue(dataJson,"errno", Integer.class);
		JSONObject json = new JSONObject();
	    if(errno==0){
	    	JSONObject so = dataJson.getJSONObject("shop");//
	    	json.put("shop_id", so.getString("shop_id"));//团单id,商户描述
	    	json.put("longitude", so.getString("longitude"));
	    	json.put("latitude", so.getString("latitude"));
	    	json.put("shop_url", so.getString("shop_url"));
	    	json.put("shop_murl", so.getString("shop_murl"));
	    	json.put("shop_name", so.getString("shop_name"));
	    	json.put("address", so.getString("address"));
	    	json.put("phone", so.getString("phone"));
	    	
	    	
	    	double a= DistanceEstimator.route(latitude, longitude, "", json.getDouble("latitude"), json.getDouble("longitude"), "");
	    	//System.out.println(a);
	    	
        }else{
        	throw ApiError.Type.INVALID_PARAM.toException();
        }
	    return json;
	}
	
	
}
